package House;

import House.tools.HouseService;
import House.tools.Utility;

/**
 * 显示界面
 * 接受用户的输入
 * 调用houseService完成对房屋信息的各种操作
 */
public class HouseView {
    private boolean loop = true;//控制显示菜单
    public char key = ' ';//接收用户的选择
    private HouseService houseService = new HouseService(10);

    public void mainMenu() {
        do {
            System.out.println("==================房屋出租系统菜单===================");
            System.out.println("\t\t\t\t\t1.新 增 房 源");
            System.out.println("\t\t\t\t\t2.查 找 房 源");
            System.out.println("\t\t\t\t\t3.删 除 房 源");
            System.out.println("\t\t\t\t\t4.修 改 房 源");
            System.out.println("\t\t\t\t\t5.显 示 房 源");
            System.out.println("\t\t\t\t\t6.退 出 系 统");
            System.out.println("请输入你的选择（1-6）");
            key = Utility.readChar();
            switch (key) {
                case '1':
                    addHouse();
                    break;
                case '2':
                    findHouse();
                    break;
                case '3':
                    delHouse();
                    break;
                case '4':
update();
                    break;
                case '5':
listHouse();
                    break;
                case '6':
exit();
                    break;
            }
        } while (loop);
    }

    //编写listHouse显示房屋信息
    public void listHouse() {
        System.out.println("========================房屋列表============================");
        System.out.println("编号\\t\\t房主\\t\\t电话\\t\\t地址\\t\\t月租\\t\\t状态（未出租/已出租）");
        House[] houses = houseService.list();//得到所有的房屋信息
        for (int i = 0; i < houses.length; i++) {
            if (houses[i] == null) {
                break;
            }
            System.out.println(houses[i] + " ");
        }
        System.out.println("========================房屋列表显示完毕============================");
    }

    //编写添加房屋信息addHouse
    public void addHouse() {
        System.out.println("========================添加房屋=======================");
        System.out.println("姓名：");
        String name = Utility.readString(8);
        System.out.println("电话：");
        String phone = Utility.readString(12);
        System.out.println("地址：");
        String address = Utility.readString(16);
        System.out.println("月租：");
        int rent = Utility.readInt(10);
        System.out.println("状态：（已出租/未出租）");
        String state = Utility.readString(3);
        //创建一个新的House对象,注意id是系统分配的，用户不能输入
        House newHouse = new House(0, name, phone, address, rent, state);
        if (houseService.add(newHouse)) {
            System.out.println("=====================房屋添加成功========================");
        } else {
            System.out.println("====================房屋添加失败=========================");
        }
    }

    //根据id查找房源
    public void findHouse() {
        System.out.println("======================查找房屋信息====================");
        System.out.println("请输入要查找的id");
        int findId = Utility.readInt();
        //调用方法
        House house = houseService.findById(findId);
        if (house != null) {
            System.out.println(house);
        } else {
            System.out.println("没有找到你要查找的ID，请再想想");
        }
    }

    //完成删除房屋信息delHouse
    public void delHouse() {
        System.out.println("===================删除房屋信息======================");
        System.out.println("请输入待删除房屋的编号（-1退出）");
        int delId = Utility.readInt();
        if (delId == -1) {
            System.out.println("=====================放弃删除房屋信息====================");
            return;
        }
        char choice = Utility.readConfirmSelection();//方法本身就有循环判断的逻辑，必须输入y或者n否则出不来
        if (choice == 'Y') {
            if (houseService.del(delId)) {
                System.out.println("===================删除房屋成功======================");
            } else {
                System.out.println("========================编号不存在删除失败=======================");
            }
        } else {
            System.out.println("=====================放弃删除房屋信息====================");
        }
    }

    //完成退出方法exit
    public void exit() {
        //选择提供的方法完成确认
        char c = Utility.readConfirmSelection();
        if (c == 'Y') {
            loop = false;
        }
    }

    //根据id修改房屋的信息update
    public void update() {
        System.out.println("======================修改房屋信息=====================");
        System.out.println("请选择待修改的房屋编号：（-1表示退出）");
        int update = Utility.readInt();
        if (update == -1) {
            System.out.println("============你放弃了修改房屋信息============");
            return;
        }
        //根据得到的update查找对象
        House house = houseService.findById(update);
        if (house == null) {
            System.out.println("=================修改的房屋信息编号不存在===================");
            return;
        }
        System.out.println("姓名(" + house.getName() + ")");
        String name = Utility.readString(8, "");//如果用户直接回车，表示不修改信息默认

        if (!"".equals(name)) {//修改
            house.setName(name);
        }
        System.out.println("电话（" + house.getPhone() + ")");
        String phone = Utility.readString(11, "");
        if (!"".equals(house.getPhone())) {
            house.setPhone(phone);
        }
        System.out.println("地址（" + house.getAddress() + ")");
        String address = Utility.readString(18, "");
        if (!"".equals(address)) {
            house.setAddress(address);
        }
        System.out.println("租金（" + house.getRent() + ")");
        int rent = Utility.readInt(-1);
        if (rent != -1) {
            house.setRent(rent);
        }
        System.out.println("状态(" + house.getState() + "):");
        String state = Utility.readString(3, "");
        if (!"".equals(state)) {
            house.setState(state);
        }
        System.out.println("====================修改成功========================");
    }

}
